//
//  XJOAuthProgressView.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/21.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit

class XJOAuthProgressView: UIProgressView {
    
    var timer:NSTimer?
    
    var isAnim:Bool = true
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 64, width: SCREENW, height: 2))
        
        setupUI()
    }
    
    private func setupUI(){
        self.tintColor = UIColor.greenColor()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:-供外界调用的方法
    
    func startAnim() {
        self.progress = 0
        self.hidden = false
        timer = NSTimer.scheduledTimerWithTimeInterval(1/60, target: self, selector: "valueChange", userInfo: nil, repeats: true)
    }
    
    func endAnim() {
        isAnim = false
    }
    
    func removeView() {
        removeFromSuperview()
    }
    
    func valueChange() {
        
        if isAnim {
            self.progress += 0.05
            
            if self.progress >= 0.95{
                self.progress = 0.95
            }
        }else{
            //加载完成之后,让进度条消失,定时器失效
            if self.progress == 1{
                self.hidden = true
                timer?.invalidate()
            }else{
                self.progress += 0.05
            }
        }
    }
    
    deinit{
        timer?.invalidate()
    }
}
